package com.zy.batiya.util;

import java.security.MessageDigest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSONObject;
import com.zy.batiya.domain.Menu;
import com.zy.batiya.domain.Pager;

/**
 * 工具类
 * */
public class CommonUtils {
	
	/**
	 * 获取总页数重载方法
	 * */
	public static Integer getTotalPage(Pager<?> pager,Integer total){
		if(total == null)
			return null;
		int size = pager.getSize();
		int totalPage = total > 0 ? total % size != 0 ? (total / size + 1) : total / size : 0;
		return totalPage;
	}
	
	public static Integer getTotalPage(int size,Integer total){
      if(total == null)
          return null;
      int totalPage = total > 0 ? total % size != 0 ? (total / size + 1) : total / size : 0;
      return totalPage;
    }
	
	// 默认时间格式  yyyy-MM-dd hh:mm:ss
	public static final String DEFAULT_FORMAT = "yyyy-MM-dd hh:mm:ss";
	
	// 前段时间控件对应的format
	public static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd");
	public static final Pattern PATTERN = Pattern.compile("[0-9]{4}-[0-9]{1,2}-[0-9]{2}");
	
	// 获取当前时间
	public static Date getNow() throws ParseException {
		return getNow(DEFAULT_FORMAT);
	}
	
	public static String UUID(){
		return UUID.randomUUID().toString().replaceAll("-","").toLowerCase();
	}
	
	/**
	 * 获取当前时间
	 * 如果format为空     默认格式：yyyy-MM-dd hh:mm:ss
	 * @param String format 时间格式化
	 * @return Date
	 * @throws ParseException 
	 * */
	public static Date getNow(String format) throws ParseException {
		if(format == null) format = DEFAULT_FORMAT;
		SimpleDateFormat mt = new SimpleDateFormat(format);
		Date dt = mt.parse(mt.format(new Date()));
		return dt;
	}
	
	/**
	 * 将字符串md5加密
	 * */
	public static String MD5(String key) {
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (Exception e) {
			System.out.println(e.toString());
			e.printStackTrace();
			return "";
		}
		char[] charArray = key.toCharArray();
		byte[] byteArray = new byte[charArray.length];

		for (int i = 0; i < charArray.length; i++)
			byteArray[i] = (byte) charArray[i];
		byte[] md5Bytes = md5.digest(byteArray);
		StringBuffer hexValue = new StringBuffer();
		for (int i = 0; i < md5Bytes.length; i++) {
			int val = ((int) md5Bytes[i]) & 0xff;
			if (val < 16)
				hexValue.append("0");
			hexValue.append(Integer.toHexString(val));
		}
		return hexValue.toString();
	}

	/**
	 * 返回json
	 * @param code
	 * @param msg
	 * @return String
	 * */
	public static String resultJsonString(Integer code,String msg) {
		JSONObject json = new JSONObject();
		json.put("code",code);
		json.put("msg",msg);
		return json.toJSONString();
	}
	
	public static boolean isNull(String str){
		return str == null || "".equals(str) || "null".equals(str) ? true : false;
	}
	
	public static String successResult(List<Menu> list) {
		JSONObject json = new JSONObject();
		json.put("code", 0);
		json.put("data",list);
		return json.toJSONString();
	}
}
